//----------------------------------------------------------------------------
// View 'Historie'
//----------------------------------------------------------------------------
Ti.include('hh_recept_history_details_ios.js');



var viewHistorie = Titanium.UI.createView({
   width : '100%',
   height : '100%'
});

//----- Buttons
var btnTerug = Titanium.UI.createButton({
   title : T_HHRECEPT_HISTORY_BTN_BACK
});
btnTerug.addEventListener('click', showHHRecepten);

var btnHistorieWissen = Titanium.UI.createButton({
   title : T_HHRECEPT_HISTORY_BTN_DELETE
});
btnHistorieWissen.addEventListener('click', function(e) {
   dialogHistorieWissen.show();
});

//----- Dialogs
var dialogHistorieWissen = Titanium.UI.createAlertDialog({
   title :  T_HHRECEPT_HISTORY_DLG_TITLE,
   message :  T_HHRECEPT_HISTORY_DLG_MESSAGE,
   buttonNames : [T_OK, T_CANCEL],
   cancel : 1
});
dialogHistorieWissen.addEventListener('click', function(event) {
   if (event.index == 0) {
      deleteHHRecepten();
      populateTableHistorie();
      Ti.Analytics.featureEvent(APP_EVENTS.EVENT_HHRECEPT_HISTORY_CLEARED);
   }
   else {
      log("Historie wissen geannuleerd");
   }
});


viewHistorie.add(Titanium.UI.createLabel({
   textAlign : Ti.UI.TEXTALIGNMENTLEFT,
   font : {
      fontSize : 12,
      fontWeight : 'bold',
      fontFamily : customFont
   },
   color : hh_lichtblauw,
   top : '15',
   left : '15',
   right : '15',
   text : T_HHRECEPT_HISTORY_TITLE
}));


var viewHistorieContent = Titanium.UI.createView({
   backgroundColor : wit,
   top : '35',
   width : '90%',
   bottom : '10',
   borderRadius : '3',
   borderColor : middenblauw
});

var tableHistorie = Ti.UI.createTableView();
populateTableHistorie();
tableHistorie.addEventListener('click', function(e) {
   selectedHHreceptDT = e.rowData.title;
   showHerhaalReceptDetails(selectedHHreceptDT);
});


//----- Add UI elements
viewHistorieContent.add(tableHistorie);
viewHistorie.add(viewHistorieContent);



//----- Functions
function populateTableHistorie() {
   var tableData = [];
   hhreceptendb = Ti.Database.open('hhrecepten');
   try {
      var hhreceptenRS = hhreceptendb.execute("SELECT date_time FROM hhrecepten;");
      if (hhreceptenRS.getRowCount() != 0) {
         log("tableHistorie vullen met gegevens uit tabel hhrecepten");
         while (hhreceptenRS.isValidRow()) {
            tableData.push({
               title : hhreceptenRS.fieldByName('date_time'),
               hasChild : true,
               font : {
                  fontSize : 16,
                  fontFamily : customFontNormal
               },
               color : hh_lichtblauw,               
            });
            hhreceptenRS.next();
         }
      }
      else {
         log("Tabel hhrecepten bevat geen records");
         tableHistorie.setData([]);
      }
      hhreceptenRS.close();
   }
   catch(e) {
      log(e);
      tableHistorie.setData([]);
   }
   hhreceptendb.close();
   log("tableview Historie bijwerken");
   tableHistorie.setData(tableData);
}

function showHHRecepten() {
   winHHRecept.remove(viewHistorie);
   winHHRecept.add(viewHHRecepten);
   winHHRecept.leftNavButton = btnHistorie;
   winHHRecept.rightNavButton = btnHHReceptBellen;
}

function showHerhaalReceptDetails(dt) {
   winHHRecept.add(viewHerhaalReceptDetails);
   winHHRecept.remove(viewHistorie);
   winHHRecept.leftNavButton = btnTerugNaarHistorie;
   winHHRecept.rightNavButton = '';
   winHHRecept.rightNavButton = null;
   winHHRecept.setTitle('Herhaalrecept details');
   getHHRecept(dt);
}

function deleteHHRecepten() {
   log("Herhaalrecepten worden gewist");
   hhreceptendb = Ti.Database.open('hhrecepten');
   hhreceptendb.execute('DELETE FROM hhrecepten;');
   hhreceptendb.close();
   alert(T_HHRECEPT_HISTORY_DELETED);
}



